Extract content from a document is always needed in people’s daily work. I have post an article Extract Images and Text in PDF with C#, VB.NET in my blog. Now I want to share another method to extract images from a word document with C#, VB.NET. The whole process is very easy. The main method can be finished only by three steps.
How to extract images from Word Document with C#, VB.NET
In my method, I use an MS word component Spire.Doc to finish this task. Spire. Doc supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight and doesn't need Microsoft Office Word Automation. So make sure that you Freely Install Spire.Doc in your system.
Step1. Create a new project.
Create a new project in Visual Studio and set the Target framework to be .NET Framework 4.
Step2. Add reference.
1. Add System Drawing and Spire.Doc dll as references in Project.
2. Add the below code at the top of the method.
C#
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
using Spire.Doc.Collections;
using System.Collections;
VB.NET
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Interface
Imports Spire.Doc.Collections
Imports System.Collections
How to extract images from Word Document with C#, VB.NET
In my method, I use an MS word component Spire.Doc to finish this task. Spire. Doc supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight and doesn't need Microsoft Office Word Automation. So make sure that you Freely Install Spire.Doc in your system.
Step1. Create a new project.
Create a new project in Visual Studio and set the Target framework to be .NET Framework 4.
Step2. Add reference.
1. Add System Drawing and Spire.Doc dll as references in Project.
2. Add the below code at the top of the method.
C#
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
using Spire.Doc.Collections;
using System.Collections;
VB.NET
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Interface
Imports Spire.Doc.Collections
Imports System.Collections
Step3. Extract images from word document.
1. Load a word document from system.
C# Code:
//Open a word document.
Document document = new Document(@"D:\michelle\JaneEyre.doc");
int index = 0;
VB.NET Code:
'Open a word document.
Dim document As New Document("D:\michelle\JaneEyre.doc ")
Dim index As Integer = 0
2. Extract images from word document
C# Code:
//Create a new queue.
Queue containers = new Queue();
//Put the document objects in the queue.
containers.Enqueue(document);
while (containers.Count > 0)
{
ICompositeObject container = (ICompositeObject)containers.Dequeue();
DocumentObjectCollection docObjects = container.ChildObjects;
foreach (DocumentObject docObject in docObjects)
{
//Judge the object type.
if (docObject.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture picture = docObject as DocPicture;
//Name the image.
String imageName = String.Format("Image-{0}.png", index);
//Save the image.
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
index++;
}
else
{
if (docObject is ICompositeObject)
{
containers.Enqueue(docObject as ICompositeObject);
}
}
}
}
1. Load a word document from system.
C# Code:
//Open a word document.
Document document = new Document(@"D:\michelle\JaneEyre.doc");
int index = 0;
VB.NET Code:
'Open a word document.
Dim document As New Document("D:\michelle\JaneEyre.doc ")
Dim index As Integer = 0
2. Extract images from word document
C# Code:
//Create a new queue.
Queue containers = new Queue();
//Put the document objects in the queue.
containers.Enqueue(document);
while (containers.Count > 0)
{
ICompositeObject container = (ICompositeObject)containers.Dequeue();
DocumentObjectCollection docObjects = container.ChildObjects;
foreach (DocumentObject docObject in docObjects)
{
//Judge the object type.
if (docObject.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture picture = docObject as DocPicture;
//Name the image.
String imageName = String.Format("Image-{0}.png", index);
//Save the image.
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
index++;
}
else
{
if (docObject is ICompositeObject)
{
containers.Enqueue(docObject as ICompositeObject);
}
}
}
}
VB.NET Code:
'Create a new queue.
Dim containers As New Queue(Of ICompositeObject)()
'Put the document objects in the queue.
containers.Enqueue(document)
Do While containers.Count > 0
Dim container As ICompositeObject = DirectCast(containers.Dequeue(), ICompositeObject)
Dim docObjects As DocumentObjectCollection = container.ChildObjects
For Each docObject As DocumentObject In docObjects
'Judge the object type.
If docObject.DocumentObjectType = DocumentObjectType.Picture Then
Dim picture As DocPicture = TryCast(docObject, DocPicture)
'Name the image.
Dim imageName As String = String.Format("Image-{0}.png", index)
'Save the image.
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
index += 1
Else
If TypeOf docObject Is ICompositeObject Then
containers.Enqueue(TryCast(docObject, ICompositeObject))
End If
End If
Next docObject
Loop
Preview
'Create a new queue.
Dim containers As New Queue(Of ICompositeObject)()
'Put the document objects in the queue.
containers.Enqueue(document)
Do While containers.Count > 0
Dim container As ICompositeObject = DirectCast(containers.Dequeue(), ICompositeObject)
Dim docObjects As DocumentObjectCollection = container.ChildObjects
For Each docObject As DocumentObject In docObjects
'Judge the object type.
If docObject.DocumentObjectType = DocumentObjectType.Picture Then
Dim picture As DocPicture = TryCast(docObject, DocPicture)
'Name the image.
Dim imageName As String = String.Format("Image-{0}.png", index)
'Save the image.
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
index += 1
Else
If TypeOf docObject Is ICompositeObject Then
containers.Enqueue(TryCast(docObject, ICompositeObject))
End If
End If
Next docObject
Loop
Preview
Spire.Doc can extract images in word document easily and save the images to different formats such as png, bmp, jpg, gif, tif and so on.
More Word Functions
More Word Functions